package com.feng;

import com.feng.flink.MyMapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author fengjidong
 */
@SpringBootApplication
public class SpringStartApp {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(SpringStartApp.class, args);
        batchJob(context);
    }


    public static void streamingJob(ApplicationContext context) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStream<String> source = env.socketTextStream("192.168.44.250", 8080).disableChaining();
        source.map(context.getBean(MyMapFunction.class)).print();
        env.execute();
    }

    public static void batchJob(ApplicationContext context) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> dataSource = env.fromElements("1", "3", "5", "1");
        dataSource.map(context.getBean(MyMapFunction.class)).print();
        env.execute();
    }
}
